//
//  VMControllerHelper.m
//  iOS-GMFinance-Fund
//
//  Created by ning on 2017/4/1.
//  Copyright © 2017年 胡林虎. All rights reserved.
//

#import "VMControllerHelper.h"

//针对自己的项目做特殊处理
#import "RootController.h"

@implementation VMControllerHelper

+(UINavigationController *)currentNavigationController
{
    UIViewController * vc = [VMControllerHelper currentViewController];
    if (vc.navigationController) {
        
        return vc.navigationController;
    }
    
    return nil;
}

+ (UIViewController *)currentViewController
{
    
    UIViewController *rootViewContoller = [UIApplication sharedApplication].delegate.window.rootViewController;
    
    return [VMControllerHelper currentTopViewControllerWithRootVC:rootViewContoller];
}

+ (UIViewController *)currentTopViewControllerWithRootVC:(UIViewController *)rootVC
{
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        
        UITabBarController * tvc = (UITabBarController *)rootVC;
        
        return [VMControllerHelper currentTopViewControllerWithRootVC:tvc.selectedViewController];
    }
    
    if ([rootVC isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController *nvc = (UINavigationController *)rootVC;
        return [VMControllerHelper currentTopViewControllerWithRootVC:nvc.visibleViewController];
    }
    
    if (rootVC.presentedViewController) {
        
        return [VMControllerHelper currentTopViewControllerWithRootVC:rootVC.presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[RootController class]]) {
        
        RootController *rvc = (RootController *)rootVC;
        
        return [VMControllerHelper currentTopViewControllerWithRootVC:[rvc.currentNavigator.viewControllers lastObject]];
    }
    
    return rootVC;
}

@end
